package io.renren.modules.lables.controller;

import io.renren.modules.lables.entity.UserTag;
import io.renren.modules.lables.service.UserTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class UserTagController {
    @Autowired
    private UserTagService userTagService;

    @GetMapping("/userTag")
    public String list(Model model) {
        model.addAttribute("userTags", userTagService.list());
        return "userTag/list";
    }

    @GetMapping("/userTag/{userId}")
    public String get(@PathVariable String userId, Model model) {

        List<UserTag> list = userTagService.listByUserId(userId);
        model.addAttribute("userTags", list);
        model.addAttribute("userid", userId);
        return "userTag/manage/list";
    }

    @GetMapping("/userTag/add")
    public String addForm(Model model) {
        model.addAttribute("userTag", new UserTag());
        return "userTag/add";
    }

    @PostMapping("/userTag/add")
    public String add(@ModelAttribute UserTag userTag) {
        userTag.setInsertTime(LocalDateTime.now());
        userTagService.save(userTag);
        return "redirect:/userTag/";
    }
    @PostMapping("/userTag/add/{userId}")
    public String addTag(@PathVariable String userId,@ModelAttribute UserTag userTag) {
        userTag.setInsertTime(LocalDateTime.now());
        userTag.setUserId(Long.valueOf(userId));
        userTagService.save(userTag);
        return "redirect:/userTag/{userId}";
    }

    @PutMapping("/userTag/{distinctId}")
    public String update(@PathVariable Long distinctId, @ModelAttribute UserTag userTag) {
        userTag.setDistinctId(distinctId);
        userTagService.update(userTag);
        return "redirect:/userTag/{distinctId}";
    }

    @DeleteMapping("/userTag/{distinctId}")
    public String delete(@PathVariable String distinctId) {
        userTagService.remove(distinctId);
        return "redirect:/userTag";
    }

}